package com.reborn.mall.api.service.impl;

import com.google.common.collect.Lists;
import com.reborn.mall.api.common.util.AliyunOSSUtil;
import com.reborn.mall.api.common.util.BeanUtil;
import com.reborn.mall.api.model.dto.ProductCategoryDTO;
import com.reborn.mall.api.model.entity.Product;
import com.reborn.mall.api.model.entity.ProductCategory;
import com.reborn.mall.api.mybatis.mapper.ProductCategoryMapper;
import com.reborn.mall.api.service.ProductCategoryService;
import com.reborn.mall.api.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * TODO: Here is description
 *
 * @author wu.yue
 * @since 2020/1/27
 */
@Service
public class ProductCategoryServiceImpl extends BaseServiceImpl<ProductCategory> implements ProductCategoryService {

    @Autowired
    private ProductService productService;

    @Autowired
    private AliyunOSSUtil aliyunOSSUtil;

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Override
    public ProductCategory insertSelective(ProductCategory entity) {
        if (entity.getParentId() == 0) {
            entity.setLevel(1);
        } else {
            entity.setLevel(2);
        }
        return super.insertSelective(entity);
    }

    @Override
    public List<ProductCategory> find(ProductCategory entity) {

        List<ProductCategory> all = super.findAll();
        List<ProductCategory> parents = Lists.newArrayList();
        List<ProductCategory> children = Lists.newArrayList();

        // 筛选出父级目录和子级目录
        for (ProductCategory category : all) {
            if (category.getLevel() == 1) {
                parents.add(category);
            } else {
                children.add(category);
            }
        }

        // 循环将子目录挂载到父目录的children属性上
        for (ProductCategory child : children) {
            for (ProductCategory parent : parents) {
                if (child.getParentId().equals(parent.getId())) {
                    parent.addChild(child);
                }
            }
            // 计算每个子目录下的商品数量总和
            setProductCountForChildCategory(child);
        }

        // 计算每个父目录下的商品数量总和
        for (ProductCategory parent : parents) {
            setProductCountForParentCategory(parent, children);
        }

        // 返回父级目录
        return parents;
    }

    private void setProductCountForChildCategory(ProductCategory child) {
        Product product = new Product();
        product.setProductCategoryId(child.getId());
        List<Product> products = productService.find(product);
        child.setProductCount(products.size());
    }

    private void setProductCountForParentCategory(ProductCategory parent, List<ProductCategory> children) {
        int count = 0;
        for (ProductCategory child : children) {
            if (child.getParentId().equals(parent.getId())) {
                count += child.getProductCount();
            }
        }
        parent.setProductCount(count);
    }

    @Override
    public ProductCategory createProductCategory(ProductCategoryDTO productCategoryDTO) {
        MultipartFile iconImg = productCategoryDTO.getIconImg();
        ProductCategory productCategory = BeanUtil.copyProperties(productCategoryDTO, ProductCategory.class);
        if (iconImg != null) {
            String filePath = aliyunOSSUtil.uploadFile(iconImg);
            productCategory.setIcon(filePath);
        }
        return insertSelective(productCategory);
    }
}
